var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, WxParse = require("../../wxParse/wxParse.js"), app = getApp();
let interstitialAd = null;

function e(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var i = e(require("./modules/base-canvas-palette.js")), n = e(require("../../../utils/wx-promisify/wx-promisify.js")), d = e(require("./modules/handle-create-img.js")), h = ((0, 
n.default)(wx.getSetting), 0), p = 0;

function compareV(t, a) {
    if (t && a) {
        for (var e = t.split("."), i = a.split("."), o = Math.min(e.length, i.length), s = 0, n = 0; s < o && 0 == (n = parseInt(e[s]) - parseInt(i[s])); ) s++;
        return 0 <= (n = 0 != n ? n : e.length - i.length);
    }
    return console.log("版本号不能为空"), !1;
}

Page({
    data: {
        disclaimer_show: !1,
        hide_share: 1,
        inited: !0,
        popupBoxShow: !1,
        animate: !1,
        largeStyle: "",
        shareShow: !1,
        loading: !1,
        characters: [ "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" ],
        id: 0,
        group_id: 0,
        formid: "",
        luck_pwd_show: !1,
        luck_pwd: "",
        image_text_show: !0,
        result_show: !1,
        switchs: 1,
        showState: !1,
        is_watch_ad: !1,
        videoAd: "",
        showDponsor: !1,
        mess_image: "",
        w: 370,
        creat_img: !0,
        request: 1,
        encryptedData: "",
        iv: "",
        interstitialAd: "",
        share_id: 0,
        assistance_show: !1,
        m_height: 70,
        draw_success: !1,
        showSh: !0
    },
    touchMove: function() {
        that.setData({
            showSh: !1
        });
    },
    disclaimerStatus: function() {
        this.setData({
            disclaimer_show: !this.data.disclaimer_show
        });
    },
    gotoGroupList: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/groupList/groupList?id=" + this.data.id
        });
    },
    onClosePop: function() {
        that.setData({
            draw_success: !1
        });
    },
    generatePoster: function() {
        this.data.poster = {
            z_h: 300,
            content: [ {
                type: "image",
                url: this.data.new_share.images,
                css: {
                    top: "0rpx",
                    left: "0rpx",
                    width: that.data.w + "rpx",
                    height: "220rpx",
                    mode: "scaleToFill"
                }
            }, {
                type: "text",
                text: this.data.new_share.font,
                css: Object.assign({
                    top: "230rpx",
                    left: "10rpx",
                    maxLines: 1,
                    width: that.data.w + "rpx",
                    color: "#000000",
                    fontSize: "23rpx"
                })
            }, {
                type: "text",
                text: this.data.new_share.font_text,
                css: Object.assign({
                    top: "265rpx",
                    left: "10rpx",
                    maxLines: 3,
                    width: that.data.w + "rpx",
                    color: "#ADADAD",
                    fontSize: "20rpx"
                })
            } ]
        }, console.log(this.data.poster), d.default.doCreateImg(this);
        try {
            getApp().sensors.track("XMClick", {
                ck_module: "保存图片",
                page: "投票海报"
            });
        } catch (t) {}
    },
    assistance_hide: function() {
        that.setData({
            assistance_show: !1
        });
    },
    assistance_show: function() {
        that.setData({
            assistance_show: !0
        });
    },
    imgErr: function(t) {
        console.log(22222), d.default.createImgFail(t);
    },
    onImgOK: function(t) {
        console.log(t.detail.path), that.setData({
            mess_image: t.detail.path,
            creat_img: !1
        });
    },
    showDponsor: function() {
        this.setData({
            showDponsor: !0
        });
    },
    hideDponsor: function() {
        this.setData({
            showDponsor: !1
        });
    },
    tsStart: function() {
        this.setData({
            showState: !0
        });
    },
    confirmGivingHide: function() {
        this.setData({
            showState: !1
        });
    },
    showTs: function() {
        wx.showActionSheet({
            itemList: that.data.config.complaint_content,
            success: function(t) {
                var a = that.data.config.complaint_content[t.tapIndex], e = {
                    action: "complaint",
                    contr: "luck",
                    luck_id: that.data.id,
                    token: wx.getStorageSync("token"),
                    content: a
                };
                _tools2.default.request(e, function(t) {
                    wx.showToast({
                        title: "投诉成功"
                    }), that.confirmGivingHide();
                });
            },
            fail: function(t) {
                console.log(t.errMsg);
            }
        });
    },
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        }), wx.createSelectorQuery().select("#share_more").boundingClientRect(function(t) {
            that.setData({
                m_height: t.height
            });
        }).exec();
    },
    hide: function() {
        (that = this).setData({
            loading: !0,
            popupBoxShow: !1,
            animate: !1,
            shareShow: !1
        });
    },
    shareShow: function() {
        2 == that.data.hide_share ? app.showToast("该抽奖不允许参与人分享") : this.setData({
            shareShow: !0
        });
    },
    collect: function(t) {},
    onLoad: function(t) {
        if (that = this, t.id && (this.data.id = t.id), t.group_id && this.setData({
            group_id: t.group_id
        }), t.parent_id && (wx.setStorageSync("parent_id", t.parent_id), this.data.share_id = t.parent_id), 
        t.scene) {
            var a = decodeURIComponent(t.scene).split("_");
            wx.setStorageSync("parent_id", a[0]), this.data.share_id = a[0], this.data.id = a[1], 
            2 < a.length && (this.data.group_id = a[2]);
        }
    },
    switchShow: function(t) {
        var a = t.currentTarget.dataset.switchs;
        1 == a ? this.setData({
            result_show: !0,
            image_text_show: !1
        }) : this.setData({
            result_show: !1,
            image_text_show: !0
        }), this.setData({
            switchs: a
        });
    },
    onNewShow: function() {
        var t = {
            action: "details",
            contr: "luck",
            token: wx.getStorageSync("token"),
            id: this.data.id,
            group_id: this.data.group_id,
            encryptedData: this.data.encryptedData,
            iv: this.data.iv
        };
        _tools2.default.request(t, function(t) {
            if (t.info.is_own || 2 != t.info.luck.is_share || (wx.hideShareMenu({
                menus: [ "shareAppMessage", "shareTimeline" ]
            }), console.log(2222), that.setData({
                hide_share: 2
            })), t.info.luck.image_text) {
                var a = t.info.luck.image_text;
                WxParse.wxParse("article", "html", a, that, 5);
            }
            (wx.createInterstitialAd && t.info.screen_ad && "" == that.data.interstitialAd && (that.data.interstitialAd = wx.createInterstitialAd({
                adUnitId: t.info.screen_ad
            }), that.data.interstitialAd.onError(function(t) {
                console.log(t);
            }), that.data.interstitialAd.show().catch(function(t) {
                console.error(t);
            })), 0 < t.info.group_id && 2 == t.info.luck.luck_mode && t.info.group.join && 0 < t.info.group.join.length && t.info.group.join.length < t.info.luck.team_number && (t.info.group.join.length = t.info.luck.team_number), 
            1 != t.info.luck.status && that.setData({
                image_text_show: !1,
                result_show: !0
            }), 2 == t.info.luck.is_ad && t.info.adUnitId) && (compareV(wx.getSystemInfoSync().SDKVersion, "2.0.4") && wx.createRewardedVideoAd && (that.data.videoAd = wx.createRewardedVideoAd({
                adUnitId: t.info.adUnitId
            })));
            if (1 < t.info.luck.status && t.info.is_participate && !wx.getStorageSync("luck_details_" + that.data.id) && (setTimeout(function() {
                that.setData({
                    popupBoxShow: !0,
                    animate: !0
                }), that.setData({
                    largeStyle: "transition:height .5s;height:70%;"
                });
            }, 700), wx.setStorageSync("luck_details_" + that.data.id, 1)), t.info.is_participate && 3 == t.info.luck.luck_mode) {
                var e = t.info.assistance.user ? t.info.assistance.user.length : 0;
                console.log(t.info.assistance.user);
                for (var i = e; i < 5; i++) console.log(i), t.info.assistance.user[i] = {
                    nickname: "待邀请",
                    headimgurl: "/bh_bargain/images/waitAdd.png",
                    nont: 1
                };
                console.log(t.info.assistance.user[0]), that.setData({
                    assistance: t.info.assistance
                });
            }
            that.setData(t.info), that.generatePoster();
        });
    },
    onShow: function() {
        1044 == app.globalData.scene ? wx.getShareInfo({
            shareTicket: app.globalData.shareTicket,
            success: function(t) {
                console.log(9999999999), console.log(t), that.setData({
                    encryptedData: t.encryptedData,
                    iv: t.iv
                }), that.onNewShow();
            }
        }) : this.onNewShow();
        
    },
    inputLuckPwd: function(t) {
        this.setData({
            luck_pwd: t.detail.value
        });
    },
    add_num: function() {
        var t = {
            action: "subscribe",
            contr: "my",
            token: wx.getStorageSync("token"),
            type: 1
        };
        _tools2.default.request(t, function(t) {});
    },
    joinGame: function() {
        if (!this.data.is_participate) if (that.data.dy) wx.requestSubscribeMessage({
            tmplIds: that.data.dy,
            success: function(t) {
                console.log(t), that.add_num();
            },
            fail: function(t) {
                console.log(t);
            },
            complete: function(t) {
                4 != that.data.luck.luck_mode ? that.participate() : that.setData({
                    luck_pwd_show: !0
                });
            }
        }); else {
            if (4 == this.data.luck.luck_mode) return void this.setData({
                luck_pwd_show: !0
            });
            this.participate();
        }
    },
    participate: function() {
        var a = this, t = {
            action: "participate",
            contr: "luck",
            token: wx.getStorageSync("token"),
            id: this.data.id,
            group_id: this.data.group_id,
            luck_pwd: this.data.luck_pwd,
            encryptedData: this.data.encryptedData,
            iv: this.data.iv,
            share_id: this.data.share_id
        };
        if (2 == that.data.luck.is_ad && !this.data.is_watch_ad && this.data.videoAd) return this.data.videoAd.show().catch(function() {
            that.data.videoAd.load().then(function() {
                return that.data.videoAd.show();
            }).catch(function(t) {
                console.log("激励视频 广告显示失败");
            });
        }), void this.data.videoAd.onClose(function(t) {
            a.data.videoAd && (a.data.videoAd.offClose(), (t && t.isEnded || void 0 === t) && (that.setData({
                is_watch_ad: !0
            }), that.participate()));
        });
        1 == that.data.request && (that.data.request = 2, _tools2.default.request(t, function(t) {
            (that.data.request = 1) == t.status ? (that.data.yy ? wx.showModal({
                title: "步数商城",
                content: "参与成功",
                cancelText: "算了吧",
                confirmText: "去看看",
                success: function(t) {
                    console.log(t.confirm), t.confirm ? wx.navigateTo({
                        url: "/packageA/pages/index/index"
                    }) : t.cancel && console.log("用户点击取消");
                }
            }) : 2 == that.data.hide_share ? app.showLoading("参与成功") : that.setData({
                draw_success: !0
            }), that.setData({
                luck_pwd_show: !1
            }), that.onShow()) : wx.showModal({
                title: "提示",
                mask: !0,
                content: t.info,
                showCancel: !1
            });
        }, function() {}, function() {}, "", !0));
    },
    hide_luck_pwd: function() {
        this.setData({
            luck_pwd_show: !1
        });
    },
    gotoSmall: function() {
        2 == this.data.luck.sponsor_text.type ? wx.navigateToMiniProgram({
            appId: this.data.luck.sponsor_text.appid,
            path: this.data.luck.sponsor_text.path,
            success: function(t) {}
        }) : wx.previewImage({
            current: this.data.luck.sponsor_text.image,
            urls: [ this.data.luck.sponsor_text.image ]
        });
    },
    lottery: function(t) {
        var a = {
            formid: t.detail.formId,
            action: "lottery",
            contr: "luck",
            token: wx.getStorageSync("token"),
            id: this.data.id
        };
        _tools2.default.request(a, function(t) {
            that.setData({
                luck_pwd_show: !1
            }), that.onShow();
        }, function() {}, function() {}, "", !0);
    },
    copyText: function(t) {
        var a = t.currentTarget.dataset.copy;
        wx.setClipboardData({
            data: a,
            success: function(t) {
                wx.showToast({
                    title: "复制成功"
                });
            }
        });
    },
    getUserInfo: function(t) {
        if ("getUserInfo:ok" == t.detail.errMsg) {
            var a = {
                action: "register",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: t.detail.encryptedData,
                iv: t.detail.iv
            };
            _tools2.default.request(a, function(t) {
                that.setData({
                    is_auth: !0
                });
            });
        }
    },
    gotoIndex: function() {
        wx.reLaunch({
            url: "/bh_bargain/pages/luck/luck"
        });
    },
    goToUsers: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/joinUserList/joinUserList?id=" + this.data.id
        });
    },
    gotoPrizeAddress: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/addressList/addressList?id=" + this.data.id
        });
    },
    gotoAddress: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/address/address"
        });
    },
    gotoLuckPoster: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/luckPoster/luckPoster?id=" + that.data.id
        });
    },
    gotoDrawLottery: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/drawLottery/drawLottery"
        });
    },
    gotoEmbed: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/embed/embed?id=" + this.data.id
        });
    },
    gotoAward: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/webView/webView?url=" + this.data.luck.receive_url
        });
    },
    onShareAppMessage: function() {
        wx.showShareMenu({
            withShareTicket: !0
        });
        var t = app.siteInfo.siteroot + "?i=" + app.siteInfo.uniacid + "&action=share&contr=luck&from=wxapp&c=entry&a=wxapp&do=distribute&m=bh_bargain&token=" + wx.getStorageSync("token") + "&id=" + that.data.id;
        return console.log("bh_bargain/pages/userGame/userGame?parent_id=" + this.data.share.member_id + "&id=" + that.data.id + "&group_id=" + that.data.group_id), 
        {
            title: this.data.member.nickname + "邀请你免费参与【" + this.data.luck.goods[0].name + "】抽奖",
            imageUrl: that.data.mess_image ? that.data.mess_image : t,
            path: "bh_bargain/pages/userGame/userGame?parent_id=" + this.data.share.member_id + "&id=" + that.data.id + "&group_id=" + that.data.group_id
        };
    }
});